package com.kye.express.business.customer_intro.detail;

import android.content.Context;
import android.text.TextUtils;

import com.kye.express.R;
import com.kye.express.contract.base.BasePresenter;
import com.kye.express.contract.base.view.RxView;
import com.kye.model.OpenApiResultObserver;
import com.kye.model.UserSession;
import com.kye.model.base.RetrofitManager;
import com.kye.model.bean.OpenApiResult;
import com.kye.model.bean.customer_intro.CustomerAppealResultBean;
import com.kye.model.bean.customer_intro.GetCustomerIntroDetailResultBean;
import com.kye.model.bean.customer_intro.GetCustomerIntroDetailResultBean;
import com.kye.model.bean.customer_intro.RequestCustomerAppealBean;
import com.kye.model.bean.customer_intro.RequestCustomerIntroHistoryBean;
import com.kye.model.bean.customer_intro.RequestGetCustomerIntroDetailBean;
import com.kye.model.bean.user.User;
import com.kye.model.constant.Constants;
import com.kye.model.service.CustomerIntroService;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;

public interface CustomerIntroDetailActivityContract {

    interface CustomerIntroDetailActivityView extends RxView {

        void onComplete();

        void onLoading();

        void onLoaded(GetCustomerIntroDetailResultBean datas);

        void onError(String msg);

        void onErrorAppeal(String msg);

        void onLoadedAppeal(CustomerAppealResultBean datas);
    }

    class CustomerIntroDetailActivityViewPresenter extends BasePresenter<CustomerIntroDetailActivityContract.CustomerIntroDetailActivityView> {

        private int mCurrentPage = 1;
        private Context mContext;

        public CustomerIntroDetailActivityViewPresenter(Context context) {
            this.mContext = context;
        }

        public void loadDatas(String introductionToCompany) {
            String phone = "";
            User user = UserSession.getInstance().getUser();
            if (user != null) {
                phone = user.getPhone();
            }

            RetrofitManager.getInstance().createOpenApiService(CustomerIntroService.class)
                    .getCustomerIntroDetail(new RequestGetCustomerIntroDetailBean(phone, introductionToCompany))
                    .compose(getView().<OpenApiResult<GetCustomerIntroDetailResultBean>>bindToLifecycleDestroy())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe((new OpenApiResultObserver<GetCustomerIntroDetailResultBean>() {

                        @Override
                        public void onLoading(Disposable d) {
                            getView().onLoading();
                        }

                        @Override
                        public void onOtherError(Throwable e) {
                            getView().onError(mContext.getString(R.string.common_net_error));
                        }

                        @Override
                        public void onLoaded() {
                            getView().onComplete();
                        }

                        @Override
                        public void onSuccess(OpenApiResult<GetCustomerIntroDetailResultBean> result, GetCustomerIntroDetailResultBean datas) {
                            if (datas != null) {
                                getView().onLoaded(datas);
                            }else {
                                getView().onError(mContext.getString(R.string.common_no_datas));
                            }
                        }

                        @Override
                        public void onApiError(OpenApiResult<GetCustomerIntroDetailResultBean> result, int code, String msg) {
                            if (!TextUtils.isEmpty(msg)) {
                                getView().onError(msg);
                            } else {
                                getView().onError(mContext.getString(R.string.common_net_error));
                            }
                        }
                    }));
        }

        public void customerAppeal(String orderNO, String appeal) {
            String phone = "";
            User user = UserSession.getInstance().getUser();
            if (user != null) {
                phone = user.getPhone();
            }

            RetrofitManager.getInstance().createOpenApiService(CustomerIntroService.class)
                    .customerAppeal(new RequestCustomerAppealBean(orderNO, phone, appeal))
                    .compose(getView().<OpenApiResult<CustomerAppealResultBean>>bindToLifecycleDestroy())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe((new OpenApiResultObserver<CustomerAppealResultBean>() {

                        @Override
                        public void onLoading(Disposable d) {
                            getView().onLoading();
                        }

                        @Override
                        public void onOtherError(Throwable e) {
                            getView().onErrorAppeal(mContext.getString(R.string.common_net_error));
                        }

                        @Override
                        public void onLoaded() {
                            getView().onComplete();
                        }

                        @Override
                        public void onSuccess(OpenApiResult<CustomerAppealResultBean> result, CustomerAppealResultBean datas) {
                            getView().onLoadedAppeal(datas);
                        }

                        @Override
                        public void onApiError(OpenApiResult<CustomerAppealResultBean> result, int code, String msg) {
                            if (!TextUtils.isEmpty(msg)) {
                                getView().onErrorAppeal(msg);
                            } else {
                                getView().onErrorAppeal(mContext.getString(R.string.common_net_error));
                            }
                        }
                    }));
        }
    }
}
